/*
 * Copyright 2009 (c) Patrick Pietens
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package indigo.utils 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	/**
	 * The DisposableEventDispatcher extends Flash its own EventDispatcher. It stores
	 * references to all its listeners and events. So when the DisposableEventDispatcher
	 * is disposed it removes all its listeners.
	 * 
	 */
	public class DisposableEventDispatcher extends EventDispatcher implements IDisposable, IDisposableEventDispatcher
	{
		/**
		 * Boolean that indicates the DisposableEventDispatcher is disposed. 
		 * Calling method and/or properties on a disposed EventDIspather will 
		 * cause runtime errors. Use this property to check if the EventDispatcher
		 * is disposed.
		 */
		private var _isDisposed:Boolean = false;		
		/**
		 * Array that holds all listeners of the DisposableEventDispatcher
		 */
		private var _listeners:Array = new Array( );

		/**
		 * Constructor
		 * 
		 */		
		public function DisposableEventDispatcher(target:IEventDispatcher = null)
		{
			super( target );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function addEventListener( type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false ):void 
		{
			this._listeners.push( new Listener( type, listener, useCapture ) );
			super.addEventListener( type, listener, useCapture, priority, useWeakReference );
		}

		
		/**
		 * @inheritDoc
		 *
		 */
		override public function removeEventListener( type:String, listener:Function, useCapture:Boolean = false ):void 
		{	
			super.removeEventListener( type, listener, useCapture );
			
			// Set local variables
			var myListener:Listener;
			
			// Loop through all listeners
			var i:int = this._listeners.length;
			while ( --i > -1 )
			{
				myListener = this._listeners[ i ] as Listener;
				if ( myListener.type == type && myListener.listener == listener && myListener.useCapture == useCapture )
				{
					this._listeners.splice( i, 1 );
				}
			}
		}

		
		/**
		 * Removes all current listeners of the EventDispatcher
		 * 
		 */
		public function removeListeners():void
		{
			var myListener:Listener;

			var i:int = this.totalListeners;
			while ( --i > -1 )
			{
				myListener = this._listeners[ i ] as Listener;
				this.removeEventListener( myListener.type, myListener.listener, myListener.useCapture );
			}
			
			this._listeners = new Array( );
		}

		
		/**
		 * Integer that indicates the total number of listeners
		 * 
		 */
		public function get totalListeners():int
		{
			return this._listeners.length;
		}

		
		/**
		 * Boolean that indicates the DisposableEventDispatcher is disposed. 
		 * Calling method and/or properties on a disposed EventDIspather will 
		 * cause runtime errors. Use this property to check if the EventDispatcher
		 * is disposed.
		 */
		public function get isDisposed():Boolean
		{
			return this._isDisposed;		
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		public function dispose( event:Event = null ):void
		{
			if ( !this.isDisposed )
			{
				log( "Disposing DisposableEventDispatcher" );
			
				this.removeListeners( );
			
				this._listeners = null;
				this._isDisposed = true;
			}
		}

		
		/**
		 * Executes when the <code>DisposableEventDispatcher</code> is disposed
		 * 
		 */
		protected function onDispose():void
		{
		}		
	}
}


internal class Listener
{
	public var type:String;
	public var listener:Function;
	public var useCapture:Boolean = false;

	public function Listener( type:String, listener:Function, useCapture:Boolean = false )
	{
		this.type = type;
		this.listener = listener;
		this.useCapture = useCapture;
	}
}	